from flask import request, session
from flask_restful import Resource

from database import Database


class PostArticle(Resource):
    def post(self):
        db = Database()
        type = session.get('type')
        data = request.get_json()
        publisher = data['publisher']
        title = data['title']
        subtitle = data['subtitle']
        content = data['content']
        imageUrl = data['imageUrl']
        article_type = data['type']
        if type == 'manager':
            articleId = db.execute_query("SELECT * FROM articleids ")
            articleId1 = str(articleId[0])
            if articleId:
                db.execute_query(
                    "INSERT INTO `article` (articleId, type, publisher, title,subtitle,content,imgUrl) VALUES (%s,  %s, %s, %s,%s,%s,%s)",
                    (articleId1, article_type, publisher, title, subtitle, content, imageUrl))
            db.execute_query("UPDATE articleids SET articleId = articleId + 1")

            return {"message": "article created successfully"}, 200
        else:
            return {"message": "not manager"}, 200  # Changed return status to 409 Conflict
